package com.small.act.discount;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.small.R;
import com.small.adapter.FavDiscountAdapter;
import com.small.commom.Action;
import com.small.commom.AsyncDataLoader;
import com.small.commom.AsyncDataLoader.Callback;
import com.small.commom.Common;
import com.small.commom.Thinksns;
import com.small.model.DiscountDetailModel;
import com.small.model.FavDiscountModel;
import com.small.model.ShopBaseInfoModel;

public class FavDiscountActivity extends Activity {

	private ListView list_view;
	TextView tv_emepty;
	private ImageButton btn_back;
	private Button btn_editfav;

	private Action action;
	private int result;

	private HashMap<String, Object> favDiscountMap = null;
	ArrayList<FavDiscountModel> discountList = null;
	FavDiscountAdapter list_adapter;

	int intPage = 1;
	DiscountDetailModel discountModel = null;
	ArrayList<ShopBaseInfoModel> shopList = null;
	
	private boolean isFirst = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favdiscount_view);
		action = new Action(FavDiscountActivity.this);
		findView();

	}

	public void findView() {
		tv_emepty = (TextView) this.findViewById(R.id.tv_empty);
		btn_editfav = (Button) this.findViewById(R.id.btn_editfav);
		btn_editfav.setOnClickListener(onClick);
		btn_back = (ImageButton) this.findViewById(R.id.btn_back);
		btn_back.setOnClickListener(onClick);
		list_view = (ListView) this.findViewById(R.id.mylook_list);
		list_view.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				// startActivity(new
				// Intent(FavDiscountActivity.this,DiscountDetailActivity.class));
				gotoDetail(discountList.get(position).getCouponID());
			}
		});
	}

	private void gotoDetail(final int i) {
		action.showProgressDialog("正在加载详情数据...");
		new Thread() {
			public void run() {
				favDiscountMap = action.getDiscountDetail(String.valueOf(i));
				han.sendEmptyMessage(1);
			}
		}.start();
	}

	Handler han = new Handler() {

		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			action.stopProgressDialog();
			switch (msg.what) {
			case 1:
				if (favDiscountMap != null) {
					discountModel = (DiscountDetailModel) favDiscountMap
							.get("discountModel");
					shopList = (ArrayList<ShopBaseInfoModel>) favDiscountMap
							.get("ShopList");
					if (discountModel != null) {
						Intent intent = new Intent(FavDiscountActivity.this,
								DiscountDetailActivity.class);
						Bundle mBundle = new Bundle();
						mBundle.putSerializable("discountModel", discountModel);
						mBundle.putSerializable("shopList", shopList);
						intent.putExtras(mBundle);
						startActivity(intent);
					} else {
						action.showShortToast("暂无优惠券详情，请稍后再试···");
					}
				} else {
					action.showShortToast("暂无优惠券详情，请稍后再试···");
				}

				break;
			}
		}
	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (Common.model == null) {
			isFirst = true;
			Thinksns.getInstance(FavDiscountActivity.this).goToLogin();
		} else {
			if (isFirst) {
				initData();
				isFirst = false;
			} 
			
		}
	}

	private void initData() {
		action.showProgressDialog("正在加载...");
		new AsyncDataLoader(asyncCallback).execute();
	}

	private Callback asyncCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() {
			favDiscountMap = action.getFavDiscountInfo("1", "863363014570588",
					"117.15863", "36.65231", 5, 1);
		}

		@Override
		public void onPrepare() {
		}

		@SuppressWarnings("unchecked")
		@Override
		public void onFinish() {

			action.stopProgressDialog();

			if (favDiscountMap != null) {
				Integer i = (Integer) favDiscountMap.get("result");
				result = (int) i;
				discountList = (ArrayList<FavDiscountModel>) favDiscountMap
						.get("discountList");

				resultMethod(result, discountList);
			} else {
				resultMethod(-2, null);
			}

		}
	};

	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.btn_back:// 返回
				finish();
				break;
			case R.id.btn_editfav:// 编辑收藏优惠券

				if (list_adapter != null && !list_adapter.getEdit()) {
					btn_editfav.setText("完成编辑");
					list_adapter.setEdit(true);
				} else {
					btn_editfav.setText("编辑 ");
					list_adapter.setEdit(false);
				}

				break;
			}
		}
	};

	private void setEmpty(boolean t) {
		if (t) {
			tv_emepty.setVisibility(View.VISIBLE);
			list_view.setVisibility(View.GONE);
			btn_editfav.setVisibility(View.GONE);
		} else {
			tv_emepty.setVisibility(View.GONE);
			btn_editfav.setVisibility(View.VISIBLE);
			list_view.setVisibility(View.VISIBLE);
		}
	}

	protected void resultMethod(int result2,
			List<FavDiscountModel> mydiscountlist) {
		setEmpty(true);
		switch (result2) {
		case 0:
			action.showShortToast("请求失败，请稍候再试...");
			break;
		case 1:
			if (mydiscountlist != null) {
				setEmpty(false);
				list_adapter = new FavDiscountAdapter(FavDiscountActivity.this,
						mydiscountlist);
				list_view.setAdapter(list_adapter);
			} else {
				action.showShortToast("请求失败，请稍候再试...");
			}
			break;
		case -2:
			action.showShortToast("数据异常，请求失败...");
			break;

		default:
			break;
		}
	}

}
